﻿using Hospital.IService;
using Hospital.Model.type;
using Hospital.Utility.ApiResult;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Hospital.Model.Page;
using SqlSugar;

namespace Hospital.WebApi.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]

    public class JobTitleController : ControllerBase
    {
        private readonly IJobTitleService _iJobTitleService;

        public JobTitleController(IJobTitleService iJobTitleService)
        {
            _iJobTitleService = iJobTitleService;
        }

        // Get all JobTitle
        [HttpGet]
        public async Task<ActionResult<ApiResult>> GetJobTitles()
        {
            try
            {
                var JobTitles = await _iJobTitleService.QueryAsync();
                if (JobTitles != null) { return ApiResultHelper.Success(JobTitles); }
                else
                {
                    return ApiResultHelper.Error("数据为空");
                }
            }
            catch
            {
                return ApiResultHelper.Error("获取信息出现错误");
            }
        }

        // 分页查询
        [HttpGet("page")]
        public async Task<ActionResult<ApiResult>> GetJobTitlesByPage([FromQuery] PageRequest pageRequest)
        {
            try
            {
                RefAsync<int> total = 0;
                var jobTitles = await _iJobTitleService.QueryAsync(pageRequest, total);
                return ApiResultHelper.Success(jobTitles, total);
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("分页查询出现错误");
            }
        }

        // Create a new JobTitle
        [HttpPost]
        public async Task<ActionResult<ApiResult>> CreateJobTitle([FromBody] JobTitle JobTitle)
        {
            try
            {
                bool b = await _iJobTitleService.CreateAsync(JobTitle);
                if (b) return ApiResultHelper.Success("创建成功");
                return ApiResultHelper.Error("添加失败");
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("创建信息出现错误");
            }
        }

        // Update JobTitle details
        [HttpPut("{id}")]
        public async Task<ActionResult<ApiResult>> PutUpdateJobTitle(int id, [FromBody] JobTitle JobTitle)
        {
            try
            {
                if (id == JobTitle.Id)
                {
                    bool b = await _iJobTitleService.EditAsync(JobTitle);
                    if (b) return ApiResultHelper.Success("修改成功");
                    return ApiResultHelper.Error("修改失败");
                }
                else
                {
                    return ApiResultHelper.Error("id:" + id + ",JobTitleID:" + JobTitle.Id);
                }
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("修改信息出现错误");
            }
        }

        [HttpPatch("{id}")]
        public async Task<ActionResult<ApiResult>> PatchUpdateJobTitle(int id, [FromBody] JobTitle JobTitle)
        {
            if (id != JobTitle.Id)
            {
                return BadRequest();
            }
            bool result = await _iJobTitleService.EditAsync(JobTitle);
            if (result)
            {
                return NoContent(); // Successfully updated
            }
            return NotFound(); // JobTitle not found
        }

        // Delete a JobTitle by ID
        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiResult>> DeleteJobTitle(int id)
        {
            try
            {
                bool result = await _iJobTitleService.DeleteAsync(id);
                if (result)
                {
                    return ApiResultHelper.Success("删除成功"); // Successfully deleted
                }
                return ApiResultHelper.Error("删除失败"); // JobTitle not found

            }
            catch (Exception)
            {
                return ApiResultHelper.Success("删除信息出现错误");
            }
        }
    }
}